const { tool,time,api } = getApp();
Page({

    data: {
        info: {},           // 我的信息
        showName: false,    // 是否显示修改昵称弹窗
        name: '',           // 姓名
    },

    // 保存姓名
    submit() {
        let { name } = this.data;
        let { open_id } = this.data.info;
        tool.editNickname({ nickname: name, open_id }).then(res => {
            api.showToast('修改成功','success');
            this.getMyInfo();
            this.onClose();
        })
    },

    // 输入姓名
    input(e) {
        let { value } = e.detail;
        this.setData({ name: value });
    },

    // 打开弹窗
    editName() {
        this.setData({ showName: true });
    },

    // 关闭修改姓名弹窗
    onClose() {
        this.setData({ showName: false });
    },

    // 修改头像
    editAvatar() {
        api.showActionSheet(['同步微信','重新上传']).then(res => {
            api.show();
            if (res) {
                api.chooseMedia(1).then(file => {
                    tool.upFile(file[0].tempFilePath).then(avatar => {
                        this.editAvaFun(avatar);
                    })
                })
            } else {
                wx.getUserProfile({
                    desc: '登录',
                    success:(e) => {
                        let { avatarUrl } = e.userInfo;
                        this.editAvaFun(avatarUrl);
                    },
                })
            }
        })
    },

    // 修改头像函数
    editAvaFun(avatar) {
        let { open_id } = this.data.info;
        tool.editAvatar({ open_id,avatar}).then(res => {
            api.showToast('修改成功','success');
            this.getMyInfo();
            setTimeout(() => {
                api.hide();
            }, 1000);
        })
    },

    // 获取我的信息
    getMyInfo() {
        tool.getUserInfo().then(info => {
            this.setData({ info });
        })
    },

    // 页面加载
    onLoad(options) {
        this.getMyInfo();
    },
    
})